﻿namespace Lonntec.DevClient
{
    using DevExpress.XtraEditors;
    using DevExpress.XtraEditors.Controls;
    using Furious.Authentication;
    using Furious.Authorize;
    using Furious.Common;
    using Lonntec.Business;
    using Lonntec.Common;
    using Lonntec.DevClient.Properties;
    using Lonntec.UI;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Net;
    using System.Threading;
    using System.Windows.Forms;

    public class LoginForm : LonntecForm
    {
        private SimpleButton btnExit;
        private SimpleButton btnLogin;
        private ImageComboBoxEdit cbbAccts;
        private IContainer components;
        private ImageList imageList;
        private PictureEdit picLogo;
        private TextEdit txtPassword;
        private TextEdit txtUserId;

        public LoginForm()
        {
            this.InitializeComponent();
            base.GlobalObject["MainForm"] = this;
            this.imageList.Images.Add(Resources.Simg_Acct);
        }

        private void BtnLoginClick(object sender, EventArgs e)
        {
            DevAuthInfo authInfo;
            if (string.IsNullOrEmpty(this.txtUserId.Text))
            {
                LtMessageBox.MessageExclamation(MessageRes.LoginForm_UserIdIsEmpty, new object[0]);
                this.txtUserId.Focus();
            }
            else
            {
                DevAuthInfo info = new DevAuthInfo();
                info.UserName = this.txtUserId.Text;
                info.Password = this.txtPassword.Text;
                authInfo = info;
                authInfo.Properties["HostName"] = Dns.GetHostName();
                ThreadStart threadStart = delegate
                {
                    try
                    {
                        this.Invoke(new ThreadStart(delegate
                        {
                            this.cbbAccts.Enabled = false;
                            this.txtUserId.Enabled = false;
                            this.txtPassword.Enabled = false;
                            this.btnLogin.Enabled = false;
                        }));
                        ISessionManager sessionManager = this.ProxyFactory.ResolveProxy<ISessionManager>();
                        authInfo.Properties["deviceid"] = MachineInfo.GetSystemInfo();
                        MessageBox.Show(authInfo.Properties["deviceid"].ToString());

                        Passport passport = sessionManager.Login(authInfo);
                        this.GlobalObject["Session"] = passport.Session;
                        this.GlobalObject["Passport"] = passport;
                        this.ConfigElement.SetAttribute("userid", this.txtUserId.Text);
                        this.SaveConfig();
                        this.ContainerAdapter.Resolve<SessionOnlineTimer>().Start(sessionManager);
                        this.Invoke(new ThreadStart(delegate
                        {
                            this.DialogResult = DialogResult.OK;
                        }));
                    }
                    catch (Exception ex)
                    {
                        Exception curEx = ex.InnerException ?? ex;
                        this.BeginInvoke(new ThreadStart(delegate
                        {
                            LtMessageBox.MessageExclamation(MessageRes.LoginForm_LoginError, new object[]
                            {
                                curEx.Message
                            });
                            this.cbbAccts.Enabled = true;
                            this.txtUserId.Enabled = true;
                            this.txtPassword.Enabled = true;
                            this.btnLogin.Enabled = true;
                            this.txtPassword.Focus();
                        }));
                    }
                };
                new Thread(threadStart).Start();                
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.components = new Container();
            ComponentResourceManager manager = new ComponentResourceManager(typeof(LoginForm));
            this.btnExit = new SimpleButton();
            this.btnLogin = new SimpleButton();
            this.txtPassword = new TextEdit();
            this.txtUserId = new TextEdit();
            this.cbbAccts = new ImageComboBoxEdit();
            this.imageList = new ImageList(this.components);
            this.picLogo = new PictureEdit();
            this.txtPassword.Properties.BeginInit();
            this.txtUserId.Properties.BeginInit();
            this.cbbAccts.Properties.BeginInit();
            this.picLogo.Properties.BeginInit();
            base.SuspendLayout();
            this.btnExit.DialogResult = DialogResult.Cancel;
            manager.ApplyResources(this.btnExit, "btnExit");
            this.btnExit.Name = "btnExit";
            manager.ApplyResources(this.btnLogin, "btnLogin");
            this.btnLogin.Name = "btnLogin";
            this.btnLogin.Click += new EventHandler(this.BtnLoginClick);
            manager.ApplyResources(this.txtPassword, "txtPassword");
            this.txtPassword.Name = "txtPassword";
            this.txtPassword.Properties.PasswordChar = '●';
            manager.ApplyResources(this.txtUserId, "txtUserId");
            this.txtUserId.Name = "txtUserId";
            manager.ApplyResources(this.cbbAccts, "cbbAccts");
            this.cbbAccts.Name = "cbbAccts";
            this.cbbAccts.Properties.Buttons.AddRange(new EditorButton[] { new EditorButton((ButtonPredefines) manager.GetObject("cbbAccts.Properties.Buttons")) });
            this.cbbAccts.Properties.ReadOnly = true;
            this.cbbAccts.Properties.SmallImages = this.imageList;
            this.imageList.ColorDepth = ColorDepth.Depth8Bit;
            manager.ApplyResources(this.imageList, "imageList");
            this.imageList.TransparentColor = Color.Transparent;
            manager.ApplyResources(this.picLogo, "picLogo");
            this.picLogo.CausesValidation = false;
            this.picLogo.Name = "picLogo";
            this.picLogo.Properties.AllowFocused = false;
            this.picLogo.Properties.BorderStyle = BorderStyles.NoBorder;
            this.picLogo.Properties.ReadOnly = true;
            this.picLogo.Properties.ShowMenu = false;
            base.AcceptButton = this.btnLogin;
            base.Appearance.Font = new Font("微软雅黑", 9f);
            base.Appearance.Options.UseFont = true;
            manager.ApplyResources(this, "$this");
            base.AutoScaleMode = AutoScaleMode.Font;
            base.CancelButton = this.btnExit;
            base.Controls.Add(this.btnExit);
            base.Controls.Add(this.btnLogin);
            base.Controls.Add(this.txtPassword);
            base.Controls.Add(this.txtUserId);
            base.Controls.Add(this.cbbAccts);
            base.Controls.Add(this.picLogo);
            this.DoubleBuffered = true;
            base.FormBorderStyle = FormBorderStyle.FixedSingle;
            base.MaximizeBox = false;
            base.Name = "LoginForm";
            this.txtPassword.Properties.EndInit();
            this.txtUserId.Properties.EndInit();
            this.cbbAccts.Properties.EndInit();
            this.picLogo.Properties.EndInit();
            base.ResumeLayout(false);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            base.GlobalObject["MainForm"] = this;
            this.cbbAccts.Properties.Items.Add(new ImageComboBoxItem(Constants.DevApplicationTitle, 0));
            this.cbbAccts.SelectedIndex = 0;
            string value = base.ConfigElement.GetAttribute("userid");
            this.txtUserId.Text = value;
            ThreadStart threadStart = delegate
            {
                Thread.Sleep(200);
                if (!string.IsNullOrEmpty(value))
                {
                    this.BeginInvoke(new ThreadStart(delegate
                    {
                        this.txtPassword.Focus();
                    }));
                }
            };
            new Thread(threadStart).Start();
            this.picLogo.Image = Global.Instance.ContainsKey("DevLoginBackground") ? ((Image) Global.Instance["DevLoginBackground"]) : Constants.LoginBackground;
        }
    }
}

